import React, { Component } from 'react';
import { Form, Row, Col, Icon } from 'antd';
import ShowType from '../../../routes/common/showType';
import * as commonFunc from '../../Common/commonFunc';
import styles from '../../../index.less';
import * as config from '../../../utils/config';

const FormItem = Form.Item;

class TabMainTableComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      sTabId: props.app.currentPane.key, /* 当前标签 */
      componentName: props.componentName, /* 组件名 */
      masterConfig: props.masterConfig, /* 配置 */
      sId: props.sId, /* 业务数据Id */
      dataList: [], /* 数据 */
      sActiveId: props.sActiveId, /* 窗体Id */
      disabled: props.disabled, /* 是否是查看状态 */
      sModelName: '', /* 模板名称 */
      bActivate: false, /* 激活 */
      sModelMemo: '', /* 模板描述 */
      sFormId: '', /* 单据类型 */
      reserveContent: '',
      expand: false,
    };
  }

  componentWillMount() {
    /* 获取主表数据 */
    const {
      masterConfig, sActiveId, sId, componentName, sTabId,
    } = this.state;
    const { dispatch } = this.props;
    if (sId !== undefined && sId !== '') {
      const mainId = masterConfig.sId;
      /* 表Id */
      const value = { sId, pageSize: '', pageNum: '' };
      /* 业务数据Id */
      const dataUrl = `${config.server_host}business/getBusinessDataByFormcustomId/${mainId}?sModelsId=${sActiveId}`;
      dispatch({
        type: 'content/getUrlPostData',
        payload: {
          url: dataUrl, sTabId, componentName, value, formRoute: '/elecheckmodel', returnType: 'data',
        },
      });
      /* 获取主表数据 */
    }
    if (masterConfig.gdsconfigformslave !== undefined) {
      const mainConfig = masterConfig.gdsconfigformslave;
      this.setState({
        sId: sId !== undefined ? sId : '',
        sModelName: mainConfig.filter(item => (item.sName.toLowerCase() === 'smodelname'))[0],
        bActivate: mainConfig.filter(item => (item.sName.toLowerCase() === 'bactivate'))[0],
        sModelMemo: mainConfig.filter(item => (item.sName.toLowerCase() === 'smodelmemo'))[0],
        sFormId: mainConfig.filter(item => (item.sName.toLowerCase() === 'sformid'))[0],
      });
    }
  }

  componentWillReceiveProps(nextProps) {
    const {
      sId, disabled, content, dataSet,
    } = nextProps;
    const { dataList } = content;
    const {
      sDataListTabId, returnType, urlData, loading, componentName,
    } = dataList;
    this.setState({ disabled, dataList: dataSet });
    if (this.state.sTabId === sDataListTabId && this.state.sTabId === nextProps.app.currentPane.key
      && this.state.componentName === componentName) {
      if (loading) return;
      if (returnType === 'data') {
        this.setState({
          dataList: urlData,
          sId: sId !== undefined ? sId : '',
        });
      }
      this.setState({ sId });
    }
    const mainConfig = this.state.masterConfig.gdsconfigformslave;
    if (Object.keys(mainConfig).length > 0) {
      const { sActiveId, app, form } = nextProps;
      const childProps = {
        app, form, sActiveId, sId, dataList, disabled,
      };
      const reserveContent = commonFunc.Reserve(mainConfig, childProps);
      this.setState({ reserveContent });
    }
  }

  shouldComponentUpdate(nextProps, nextState) {
    const { componentName } = nextProps;
    const { sId } = this.props;
    if (sId === undefined) {
      return true;
    }
    let flag = true;
    if (nextState.disabled !== this.state.disabled) {
      return true;
    }
    if (this.state.sTabId === nextProps.app.currentPane.key &&
      this.state.componentName === componentName) {
      flag = true;
    } else {
      flag = false;
    }
    return flag;
  }

  handleToggle = () => {
    const { expand } = this.state;
    this.setState({ expand: !expand });
  };

  render() {
    const formItemLayout = { labelCol: { span: 10 }, wrapperCol: { span: 12 } };
    const {
      app, form, handleModalPane, dataName, dataType, handleSelectChange, onClose,
    } = this.props;
    const {
      sModelName, bActivate, sModelMemo, sFormId,
    } = this.state;
    const {
      dataList, sId, sActiveId, disabled, reserveContent, expand,
    } = this.state;
    const childProps = {
      app,
      form,
      sActiveId,
      dataList,
      sId,
      disabled,
      handleModalPane,
      handleSelectChange,
      dataName,
      dataType,
      onClose,
    };
    const sModelNameProps = { showConfig: sModelName, ...childProps };
    const bActivateProps = { showConfig: bActivate, ...childProps };
    const sModelMemoProps = { showConfig: sModelMemo, ...childProps };
    const sFormIdProps = { showConfig: sFormId, ...childProps };
    return (
      <FormItem className={styles.searchMainForm}>
        <Row type="flex" style={{ height: expand ? 'auto' : '100px', overflow: 'hidden' }}>
          <Col
            span={6}
            order={sModelName.iOrder > 24 ? 24 : sModelName.iOrder}
            style={{ display: sModelName.bVisible ? 'block' : 'none' }}
          >
            <Row>
              <Col span={17}>
                <ShowType sName="sModelName" showDefaultName="模板名称" {...sModelNameProps} formItemLayout={formItemLayout} />
              </Col>
              <Col span={7}>
                <ShowType sName="bActivate" {...bActivateProps} labelNo labelName="激活" />
              </Col>
            </Row>
          </Col>
          <Col
            span={6}
            order={sModelMemo.iOrder > 24 ? 24 : sModelMemo.iOrder}
            style={{ display: sModelMemo.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sModelMemo" showDefaultName="模板描述" {...sModelMemoProps} />
          </Col>
          <Col
            span={6}
            order={sFormId.iOrder > 24 ? 24 : sFormId.iOrder}
            style={{ display: sFormId.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sFormId" showDefaultName="单据类型" {...sFormIdProps} />
          </Col>
          {reserveContent}
        </Row>
        <button className={styles.collapseA} onClick={this.handleToggle}>
          <span>{expand ? '缩略展示' : '显示全部'}</span><Icon type={expand ? 'up' : 'down'} />
        </button>
      </FormItem>
    );
  }
}

export default TabMainTableComponent;
